package com.etc.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.etc.entity.FU;
import com.etc.entity.FosterFamily;
import com.etc.entity.Orders;
import com.etc.entity.User;
import com.etc.service.FosterFamilyService;
import com.etc.service.OrdersService;
import com.etc.service.UserService;
import com.etc.tools.Msg;
import com.etc.tools.PageData;

@Controller
public class FosterFamilyController {
	@Resource(name = "fosterFamilyService")
	private FosterFamilyService fService;
	@Resource(name = "userService")
	private UserService userService;
	@Resource(name = "ordersService")
	private OrdersService os;
	@RequestMapping(value = "fosterFamilyBypage", method = RequestMethod.GET)
	@ResponseBody
	public PageData<FU> getFosterFamily(Integer page, Integer limit, String content, HttpServletRequest request) {
		if (content == null) {

			content = "";
		}
		PageData<FU> pd = fService.getFosterFamilysByPage(page, limit, content);
		pd.setCode(0);
		pd.setMsg("请求成功");
		return pd;
	}

	@RequestMapping(value = "fosterFamilyBypage1", method = RequestMethod.GET)
	@ResponseBody
	public PageData<FU> getFosterFamily1(Integer page, Integer limit, String content, HttpServletRequest request,
			HttpServletResponse response) {
		if (content == null) {

			content = "";
		}
		System.out.println(page + "," + limit);
		PageData<FU> pd = fService.getFosterFamilysByPage1(page, limit, content);
		pd.setCode(0);
		pd.setMsg("请求成功");
		request.getSession().setAttribute("pd", pd);
		return pd;
	}

	@RequestMapping(value = "fosterFamilyById", method = RequestMethod.GET)
	@ResponseBody
	public FosterFamily getUserById1(int fid) {
		FosterFamily f = fService.getFosterFamilyById(fid);
		return f;
	}

	@RequestMapping(value = "fosterFamilyByfuid", method = RequestMethod.GET)
	@ResponseBody
	public Msg<String> getUserByfuid(int fuid) {
		List<FosterFamily> f = fService.getFosterFamilyByfuid(fuid);
		User user = userService.selectByPrimaryKey(fuid);
		Msg<String> cm = new Msg<>();
		if (user==null) {
			cm.setMsg("用户不存在");
		}else {
			
			cm.setMsg((f.size() > 0) ? "寄养家庭已存在" : "不存在");
		}
		return cm;
	}

	@RequestMapping(value = "updatefosterFamilyById", method = RequestMethod.GET)
	@ResponseBody
	public Msg<String> updatefosterFamilyById(int fid, int fstatus) {
		FosterFamily fosterFamily = fService.getFosterFamilyById(fid);
		User user = userService.selectByPrimaryKey(fosterFamily.getFuid());
		if (fstatus == 0) {
			fosterFamily.setFstatus(1);
			if (user.getUlevel() != 0) {

				user.setUlevel(2);
			}
		} else {
			user.setUlevel(1);
			if (user.getUlevel() != 0) {

				fosterFamily.setFstatus(0);
			}

		}

		Msg<String> cm = new Msg<>();
		boolean u = fService.updateFosterFamily(fosterFamily);
		int f = userService.updateByPrimaryKeySelective(user);
		cm.setMsg((u && f > 0) ? "已修改" : "修改失败");
		return cm;
	}

	@RequestMapping(value = "FosterFamily/{fuid}/{fid}", method = RequestMethod.DELETE)
	@ResponseBody
	public Msg<String> delUser(@PathVariable("fuid") int fuid,@PathVariable("fid") int fid) {
		Msg<String> cm = new Msg<>();
		User user=userService.selectByPrimaryKey(fuid);
		String otuname=user.getUname();
		List<Orders> list=os.getOrdersByUname(otuname);
		if(list.size()>0) {
			cm.setMsg("已有订单未完成，删除失败");
			return cm;
		}
		boolean u = fService.delFosterFamily(fid);
		if(u) {
			user.setUstatus(1);
			userService.updateByPrimaryKeySelective(user);
		}
		cm.setMsg(u ? "删除成功" : "删除失败");
		return cm;
	}

	@RequestMapping(value = "fosterFamily", method = RequestMethod.POST)
	@ResponseBody
	public Msg<String> updateUser(HttpServletRequest request, Integer fid, Integer fuid, String fintroduction,
			String frecepettype, String fservices, Integer fexperience, Integer fstatus) {
		MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
		MultipartFile file = req.getFile("fenvimageurl");
		String leixing = file.getOriginalFilename();
		Msg<String> cm = new Msg<>();
		if (!leixing.isEmpty()) {
			String type = "jpg,jpeg,png";
			String upload = leixing.substring(leixing.indexOf(".") + 1, leixing.length());
			if (type.contains(upload)) {
				String despath = "E:/fenvimageurl" + "/" + file.getOriginalFilename();
				FosterFamily fosterFamily = new FosterFamily();
				fosterFamily.setFenvimageurl(file.getOriginalFilename());
				fosterFamily.setFexperience(fexperience);
				fosterFamily.setFid(fid);
				fosterFamily.setFintroduction(fintroduction);
				fosterFamily.setFrecepettype(frecepettype);
				fosterFamily.setFservices(fservices);
				fosterFamily.setFstatus(fstatus);
				fosterFamily.setFuid(fuid);

				File desfile = new File(despath);
				File desfile1 = new File("E:/fenvimageurl");
				// 判断路径是否存在，如果不存在就创建一个
				if (!desfile1.exists()) {
					desfile1.mkdirs();
				}

				try {
					FileUtils.copyInputStreamToFile(file.getInputStream(), desfile);

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				boolean u = fService.updateFosterFamily(fosterFamily);

				cm.setMsg(u ? "修改成功" : "修改失败");
			} else {
				cm.setMsg("上传文件类型不符合");

			}

		} else {
			fService.getFosterFamilyById(fid);
			FosterFamily fosterFamily1 = fService.getFosterFamilyById(fid);
			FosterFamily fosterFamily = new FosterFamily();
			fosterFamily.setFenvimageurl(fosterFamily1.getFenvimageurl());
			fosterFamily.setFexperience(fexperience);
			fosterFamily.setFid(fid);
			fosterFamily.setFintroduction(fintroduction);
			fosterFamily.setFrecepettype(frecepettype);
			fosterFamily.setFservices(fservices);
			fosterFamily.setFstatus(fstatus);
			fosterFamily.setFuid(fuid);

			boolean u = fService.updateFosterFamily(fosterFamily);

			cm.setMsg(u ? "修改成功" : "修改失败");
		}
		return cm;
	}

	@RequestMapping(value = "fosterFamilyadd", method = RequestMethod.POST)
	@ResponseBody
	public Msg<String> addFosterFamily(HttpServletRequest request, Integer fid, Integer fuid, String fintroduction,
			String frecepettype, String fservices, Integer fexperience, Integer fstatus) {
		MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
		MultipartFile file = req.getFile("fenvimageurl");
		String leixing = file.getOriginalFilename();
		String type = "jpg,jpeg,png";
		String upload = leixing.substring(leixing.indexOf(".") + 1, leixing.length());
		Msg<String> cm = new Msg<>();
		if (type.contains(upload)) {
			String despath = "E:/fenvimageurl" + "/" + file.getOriginalFilename();
			FosterFamily fosterFamily = new FosterFamily();
			fosterFamily.setFenvimageurl(file.getOriginalFilename());
			fosterFamily.setFexperience(fexperience);
			fosterFamily.setFid(fid);
			fosterFamily.setFintroduction(fintroduction);
			fosterFamily.setFrecepettype(frecepettype);
			fosterFamily.setFservices(fservices);
			fosterFamily.setFstatus(fstatus);
			fosterFamily.setFuid(fuid);

			File desfile = new File(despath);
			File desfile1 = new File("E:/fenvimageurl");
			// 判断路径是否存在，如果不存在就创建一个
			if (!desfile1.exists()) {
				desfile1.mkdirs();
			}

			try {
				FileUtils.copyInputStreamToFile(file.getInputStream(), desfile);

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			boolean u = fService.addFosterFamily(fosterFamily);

			cm.setMsg(u ? "增加成功" : "增加失败");
		} else {
			cm.setMsg("上传文件类型不符合");

		}

		return cm;
	}

	@RequestMapping(value = "fosterFamilyApply", method = RequestMethod.POST)
	@ResponseBody
	public Msg<String> Apply(HttpServletRequest request, Integer fuid, String fintroduction, String frecepettype,
			String fservices, Integer fexperience, Integer fstatus) {
		List<FosterFamily> list=fService.getFosterFamilyByfuid(fuid);
		int flag=list.size();
		Msg<String> cm = new Msg<>();
		if(flag==0) {
			MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
			MultipartFile file = req.getFile("fenvimageurl");
			String leixing = file.getOriginalFilename();
			String type = "jpg,jpeg,png,JPG";
			String upload = leixing.substring(leixing.indexOf(".") + 1, leixing.length());			
			if (type.contains(upload)) {
				String despath = "E:/fenvimageurl" + "/" + file.getOriginalFilename();
				FosterFamily fosterFamily = new FosterFamily();
				fosterFamily.setFenvimageurl(file.getOriginalFilename());
				fosterFamily.setFexperience(fexperience);
				fosterFamily.setFintroduction(fintroduction);
				fosterFamily.setFrecepettype(frecepettype);
				fosterFamily.setFservices(fservices);
				fosterFamily.setFstatus(0);
				fosterFamily.setFuid(fuid);

				File desfile = new File(despath);
				File desfile1 = new File("E:/fenvimageurl");
				// 判断路径是否存在，如果不存在就创建一个
				if (!desfile1.exists()) {
					desfile1.mkdirs();
				}

				try {
					FileUtils.copyInputStreamToFile(file.getInputStream(), desfile);

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				boolean u = fService.addFosterFamily(fosterFamily);

				cm.setMsg(u ? "已申请" : "申请失败");
			} else {
				cm.setMsg("上传文件类型不符合");

			}
		}else {
			cm.setMsg("正在审核中，勿重复提交");
		}
		

		return cm;
	}

	@RequestMapping(value = "allFosterFamily", method = RequestMethod.GET)
	@ResponseBody
	public List<FU> getAllFosterFamily() {
		List<FU> list = fService.selectAllFosterFamily();
		return list;
	}

}
